#include "Geometry.h"


const Geometry::uint Geometry::vao() const
{
	return this->VAO;
}

const Geometry::uint Geometry::vertexCount() const
{
	return indices.size();
}

Geometry::~Geometry()
{
	destroy();
}

Geometry::Geometry() noexcept:VAO(0),VBO(0),EBO(0)
{}

void Geometry::setup()
{
	glGenVertexArrays(1,&VAO);//生成对象
	glGenBuffers(1,&VBO);
	glGenBuffers(1,&EBO);
	glBindVertexArray(VAO);

	//传送数据
	glBindBuffer(GL_ARRAY_BUFFER,VBO);
	glBufferData(GL_ARRAY_BUFFER,vertices.size()*sizeof(Vertex),vertices.data(),GL_DYNAMIC_DRAW);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint), indices.data(), GL_STATIC_DRAW);

	// 设置顶点属性指针
	// Position
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), nullptr);
	glEnableVertexAttribArray(0);

	// TexCoords
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, TexCoords));
	glEnableVertexAttribArray(1);
}

void Geometry::destroy()
{
	glDeleteBuffers(1, &VBO);
	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &EBO);
	VAO=0;
}
